package cn.gjxblog.controller;

import cn.gjxblog.common.model.HttpResult;
import cn.gjxblog.common.model.PageCondition;
import cn.gjxblog.common.utils.UUIDUtils;
import cn.gjxblog.model.entity.User;
import cn.gjxblog.model.entity.UserMsg;
import cn.gjxblog.service.UserMsgService;
import cn.gjxblog.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2018/12/7 16:22
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMsgService userMsgService;

    @PostMapping("/addUserMsg")
    public HttpResult addUserMsg(@RequestBody UserMsg userMsg){
        userMsg.setId(UUIDUtils.get32LUUid());
        userMsg.setCreateTime(new Date());

        userMsgService.addUserMsg(userMsg);
        return HttpResult.success();
    }

    @PostMapping("/queryUserMsgByUserId")
    public HttpResult<PageInfo<UserMsg>> queryUserMsgByUserId(@RequestParam String userId, @RequestBody PageCondition pageCondition){
        PageInfo<UserMsg> pageInfo = userMsgService.queryUserMsgByUserId(userId,pageCondition);
        return HttpResult.success(pageInfo);
    }

    @PostMapping("/queryUserMsgByAgentId")
    public HttpResult<PageInfo<UserMsg>> queryUserMsgByAgentId(@RequestParam  String agentId, @RequestBody PageCondition pageCondition){
        PageInfo<UserMsg> pageInfo = userMsgService.queryUserMsgByAgentId(agentId,pageCondition);
        return HttpResult.success(pageInfo);
    }

    @GetMapping("/getUserById")
    public HttpResult<User> getUserById(@RequestParam("id") String id){
        User user = userService.getUserById(id);
        return HttpResult.success(user);
    }

    @GetMapping("/getUserByEmail")
    public HttpResult<User> getUserByEmail(@RequestParam("email") String email){
        User user = userService.getUserByEmail(email);
        return HttpResult.success(user);
    }

    @PostMapping("/queryUser")
    public HttpResult<List<User>> queryUser(@RequestBody User user){
       List<User> users = userService.getUsersByCondition(user);
       return HttpResult.success(users);
    }

    @PostMapping("/addUser")
    public HttpResult addUser(@RequestBody User user,@RequestParam("enableUrl")String enableUrl){
        userService.addUser(user,enableUrl);
        return HttpResult.success();
    }

    @GetMapping("/enable")
    public HttpResult enable(@RequestParam("key") String key){
        userService.enable(key);
        return HttpResult.success();
    }

    @PostMapping("/auth")
    public HttpResult<String> auth(@RequestBody User user){
        String token = userService.auth(user.getPhone(),user.getPasswd());
        return HttpResult.success(token);
    }

    @GetMapping("/getUserByToken")
    public HttpResult<User> getUserByToken(@RequestParam("token") String token){
        User finalUser = userService.getLoginedUserByToken(token);
        return HttpResult.success(finalUser);
    }


    @GetMapping("/logout")
    public HttpResult logout(@RequestParam("token") String token){
        userService.invalidate(token);
        return HttpResult.success();
    }

    @PostMapping("/updateUser")
    public HttpResult<User> updateUser(@RequestBody User user){
        User updateUser = userService.updateUser(user);
        return HttpResult.success(updateUser);
    }

    @GetMapping("/resetNotify")
    public HttpResult<User> resetNotify(String email,String url){
        userService.resetNotify(email,url);
        return HttpResult.success();
    }


    @GetMapping("/getEmailByKey")
    public HttpResult<String> getEmailByKey(String key){
       String email = userService.getEmailByKey(key);
       return HttpResult.success(email);
    }

    @GetMapping("/resetPwd")
    public HttpResult<User> resetPwd(String key ,String password){
        User updateUser = userService.resetPwd(key,password);
        return HttpResult.success(updateUser);
    }

}
